package com.spiderman.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Description: 爬虫网络连接工具类
 * @author Kwum
 * @date 2017年8月5日 上午11:08:25
 * @version 1.0
 */

public class ConnectionUtil {

    /**
     * 获取页面数据    通过HttpURLConnection方式
     * @param address 页面地址
     * @param code 页面编码
     * @return 页面h5内容
     * @author kwum
     */
    public static String connectByHttpURLConnection(String address, String code) {
        HttpURLConnection conn = null;
        URL url = null;
        InputStream in = null;
        BufferedReader reader = null;
        StringBuffer stringBuffer = null;
        try {
            url = new URL(address);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setDoInput(true);
            conn.connect();
            in = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in, code));
            stringBuffer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
            try {
                in.close();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取页面数据    通过Jsoup方式
     * @param address 页面地址
     * @return 页面h5内容
     * @author kwum
     */
    public static String connectByJsoup(String address) {
        StringBuilder sb = new StringBuilder();
        try {
            // 这是get方式得到的
            Document doc = Jsoup.connect(address).get();
            Elements elements = doc.getAllElements();
            for(Element element : elements){
                sb.append(element.html());
            }
            // 另外一种是post方式
            /*
             * @SuppressWarnings("unused") Document doc_Post =
             * Jsoup.connect(url) .data("query","Java") .userAgent("I am jsoup")
             * .cookie("auth","token") .timeout(10000) .post(); Elements
             * links_Post = doc.select("a[href]"); for(Element link:links_Post){
             * String linkHref = link.attr("abs:href"); String linkText =
             * link.text(); //System.out.println(linkText+":"+linkHref);
             * //map.put(linkText, linkHref); }
             */
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
